package org.qphoto;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MessagesActivity extends Activity {
	
	ListView messagelist        = null;
	List<Message> msglist       = null;
	MediaPlayer  m_MediaPlayer  = null;
	MessageArrayAdapter adapter = null;

	private ImageView m_BtnAll;
	private ImageView m_BtnMe;
	private ImageView m_BtnMsg;

	 @Override
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        setContentView(R.layout.friendsmode);
	        
	        UserInfoDBHelper userdbhelper = CoreData.getCoreData().getUserInfoDBHelper();
	        MessageDBHelper  msgdbhelper  =  CoreData.getCoreData().getMessageDBHelper();
	        
	        // get login user and messages
	        UserInfo userinfo = CoreData.getCoreData().getLoginuser();
	        msglist      = msgdbhelper.GetFriendsAndMeMessages(
	        			   userinfo.getUserid(),0,0);
	        
	        messagelist  = (ListView) this.findViewById(R.id.friendsmsgcontainer);
	        adapter      = new MessageArrayAdapter(this,msglist);
	        adapter.setOnItemChildCreateListener(itemChildCreateListener);
	      
	        messagelist.setAdapter(adapter);
	        messagelist.setOnItemClickListener(listItemClickListener);
	        
	        m_BtnAll = (ImageView) findViewById(R.id.imageView2);
	        m_BtnMe  = (ImageView) findViewById(R.id.imageView3);
	        m_BtnMsg = (ImageView) findViewById(R.id.imageView4);
	        m_BtnAll.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					 reset_Btn();
					m_BtnAll.setImageResource(R.drawable.titlebtn_all_pressed);
					click_Btn_All();
				}
			});
	        m_BtnMe.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					 reset_Btn();
					m_BtnMe.setImageResource(R.drawable.titlebtn_me_pressed);
					click_Btn_Me();
				}
			});
	        m_BtnMsg.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					 reset_Btn();
					m_BtnMsg.setImageResource(R.drawable.titlebtn_msg_pressed);
					click_Btn_Msg();
				}
			});
	 }
	 
	 private void reset_Btn(){
		 m_BtnAll.setImageResource(R.drawable.titlebtn_all_normal);
		 m_BtnMe.setImageResource(R.drawable.titlebtn_me_normal);
		 m_BtnMsg.setImageResource(R.drawable.titlebtn_msg_normal);
	 }
	 
	private void click_Btn_All() {
		msglist.clear();
		MessageDBHelper msgdbhelper = CoreData.getCoreData()
				.getMessageDBHelper();

		// get login user and messages
		UserInfo userinfo = CoreData.getCoreData().getLoginuser();
		List<Message> listtmp = msgdbhelper.GetFriendsAndMeMessages(
				userinfo.getUserid(), 0, 0);
		Iterator<Message> iter = listtmp.iterator();
		while (iter.hasNext())
			msglist.add(iter.next());
		adapter.notifyDataSetChanged();
	}

	private void click_Btn_Me() {

		msglist.clear();
		MessageDBHelper msgdbhelper = CoreData.getCoreData()
				.getMessageDBHelper();

		// get login user and messages
		UserInfo userinfo     = CoreData.getCoreData().getLoginuser();
		List<Message> listtmp = msgdbhelper.GetFriendsMessages(userinfo.getUserid(), 0, 0);
		Iterator<Message> iter= listtmp.iterator();
		while(iter.hasNext())
			msglist.add( iter.next() );
		adapter.notifyDataSetChanged();
	}

	 private void click_Btn_Msg(){
		 msglist.clear();
		 adapter.notifyDataSetChanged();
	 }
	 
	 OnItemClickListener listItemClickListener = new OnItemClickListener()
	 {

		@Override
		public void onItemClick(AdapterView<?> l, View v, int position, long id) {
			// TODO Auto-generated method stub
			
		}
		 
	 };
	 
	 OnItemChildCreateListener itemChildCreateListener = new OnItemChildCreateListener()
	 {

		@Override
		public void OnCreateChildView(View item, int position, View child,
				int childid) {
			// TODO Auto-generated method stub
			
			final Message message = msglist.get(position);
			if ( childid == R.id.avator )
			{
				child.setOnClickListener( new View.OnClickListener(){

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
		        		Bundle bundle = new Bundle();
		        		bundle.putLong("userid",message.getUserid());
		        		
		        		Intent showNextPage_Intent = new Intent();
		        		showNextPage_Intent.putExtras(bundle); 
		        		showNextPage_Intent.setClass(MessagesActivity.this,UserMoodActivity.class);
		        		startActivity(showNextPage_Intent);
					}} );
			} else if( childid == R.id.msgimage )
			{
				child.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						if (message.getImageuri().equals("") == false) {
							Intent intent = new Intent();
							intent.setClass(MessagesActivity.this,
									GifPlayerActivity.class);

							Bundle bundle = new Bundle();
							bundle.putString("imgpath", message.getImageuri());
							intent.putExtras(bundle);
							startActivity(intent);
						} else if (message.getAudiouri().equals("") == false) {
							String respath = message.getAudiouri();
							try {
								m_MediaPlayer = new MediaPlayer();
								m_MediaPlayer.setOnCompletionListener(new OnCompletionListener() {
											public void onCompletion(
													MediaPlayer player) {
												m_MediaPlayer.release();
												m_MediaPlayer = null;
											}
										});

								m_MediaPlayer.setDataSource(respath);
								m_MediaPlayer.prepare();
								m_MediaPlayer.start();
							} catch (IllegalArgumentException e) {
								e.printStackTrace();
							} catch (IllegalStateException e) {
								e.printStackTrace();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}
				});
			}
		}
	 };
}
